package com.cssw.fyzb.tps.service.im.common;

import com.alibaba.fastjson.JSON;
import com.cssw.fyzb.tps.util.StringUtil;
import lombok.Data;

import java.util.*;

/**
 * @Author Jian Qin.
 */
@Data
public class ImMessage {
    private boolean syncOtherMachine = false;

    private String from_Account;

    private String[] to_Account;

    private String groupId;

    private Integer msgLifeTime;

    private String msgPriority;

    private boolean forbidBeforeSendMsgCallback;

    private boolean forbidAfterSendMsgCallback;

    private String msgType;

    private String title;

    private String desc;

    private String ext;

    private String androidInfo_Sound;

    private String androidInfo_HuaWeiChannelID;

    private String androidInfo_XiaoMiChannelID;

    private String androidInfo_OPPOChannelID;

    private String androidInfo_GoogleChannelID;

    private String apnsInfo_Sound;

    private Integer apnsInfo_BadgeMode;

    private String apnsInfo_Title;

    private String apnsInfo_SubTitle;

    private String apnsInfo_Image;

    private String[] tagsOr = null;

    private String[] tagsAnd = null;

    private Map attrsOr = null;

    private Map attrsAnd = null;


    private Map condition(){
        Map<String, Object> con = new HashMap<>();
        if(tagsAnd != null && tagsAnd.length > 0){
            con.put("TagsAnd", tagsAnd);
        }

        if(tagsOr != null && tagsOr.length > 0){
            con.put("TagsOr", tagsOr);
        }

        if(attrsAnd != null){
            con.put("AttrsAnd", attrsAnd);
        }

        if(attrsOr != null){
            con.put("AttrsOr", attrsOr);
        }

        return con;
    }


    private Map offlinePushInfo(){
        Map<String, Object> off = new HashMap<>();

        off.put("PushFlag", 0);
        off.put("Title", this.title);
        off.put("Desc", this.desc);
        off.put("Ext", this.desc);

        Map<String, Object> android = new HashMap<>();
        android.put("Sound", this.androidInfo_Sound);
        if(!StringUtil.isBlank(androidInfo_HuaWeiChannelID)){
            android.put("HuaWeiChannelID", androidInfo_HuaWeiChannelID);
        }else if(!StringUtil.isBlank(androidInfo_XiaoMiChannelID)){
            android.put("XiaoMiChannelID", androidInfo_XiaoMiChannelID);
        }else if(!StringUtil.isBlank(androidInfo_OPPOChannelID)){
            android.put("OPPOChannelID", androidInfo_OPPOChannelID);
        }else if(!StringUtil.isBlank(androidInfo_GoogleChannelID)){
            android.put("GoogleChannelID", androidInfo_GoogleChannelID);
        }

        off.put("AndroidInfo", android);

        Map<String, Object> apns = new HashMap<>();
        apns.put("Sound", apnsInfo_Sound);
        apns.put("BadgeMode", apnsInfo_BadgeMode);
        apns.put("Title", apnsInfo_Title);
        apns.put("SubTitle", apnsInfo_SubTitle);
        apns.put("Image", apnsInfo_Image);

        off.put("ApnsInfo", apns);

        return off;
    }
    protected Map msgContent(){
        return null;
    }

    protected List msgBody(){
        List<Map> bodys = new ArrayList<>();
        Map body = new HashMap();
        body.put("MsgType", getMsgType());
        body.put("MsgContent", msgContent());
        bodys.add(body);
        return bodys;
    }

    public String toJson(){
        Date date = new Date();
        Random random = new Random(date.getTime());

        Map pack = new HashMap();

        if(this.syncOtherMachine){
            pack.put("SyncOtherMachine", 2);
        }

        if(!StringUtil.isBlank(this.from_Account)){
            pack.put("From_Account", this.from_Account);
        }

        if(!StringUtil.isBlank(groupId)){
            pack.put("GroupId", groupId);
        }
        else if(this.to_Account.length == 1){
            pack.put("To_Account", this.to_Account[0]);
        }else{
            pack.put("To_Account", this.to_Account);
        }

        if(!StringUtil.isBlank(msgPriority)){
            pack.put("MsgPriority", msgPriority);
        }


        if(msgLifeTime != null){
            pack.put("MsgLifeTime", this.msgLifeTime.intValue());
        }


        pack.put("MsgRandom", random.nextInt(Integer.MAX_VALUE));

        pack.put("MsgTimeStamp", date.getTime() / 1000);

        List<String> forbids = new ArrayList<>();
        if(forbidBeforeSendMsgCallback){
            forbids.add("ForbidBeforeSendMsgCallback");
        }
        if(forbidAfterSendMsgCallback){
            forbids.add("ForbidAfterSendMsgCallback");
        }
        if(forbids.size() > 0){
            pack.put("ForbidCallbackControl", forbids);
        }

        pack.put("Condition", condition());

        pack.put("MsgBody", msgBody());

        if(this.msgLifeTime != null && this.msgLifeTime != 0){
            pack.put("OfflinePushInfo", offlinePushInfo());
        }


        return JSON.toJSONString(pack);
    }

    @Data
    public static class ImTextMessage extends ImMessage{
        private String msgContent;

        public ImTextMessage(){
            this.setMsgType("TIMTextElem");
        }

        @Override
        protected Map msgContent(){
            Map content = new HashMap();
            content.put("Text", this.msgContent);
            return content;
        }
    }

    @Data
    public static class ImLocationMessage extends ImMessage{
        private String desc;

        private Double latitude;

        private Double longitude;

        public ImLocationMessage(){
            this.setMsgType("TIMLocationElem");
        }

        @Override
        protected Map msgContent(){
            Map content = new HashMap();
            content.put("Desc", this.desc);
            content.put("Latitude", latitude);
            content.put("Longitude", longitude);
            return content;
        }
    }

    @Data
    public static class ImFaceMessage extends ImMessage{

        private String data;

        private int index;

        public ImFaceMessage(){
            this.setMsgType("TIMFaceElem");
        }

        @Override
        public Map msgContent(){
            Map content = new HashMap();
            content.put("Index", this.index);
            content.put("Data", this.data);
            return content;
        }
    }

    @Data
    public static class ImCustomMessage extends ImMessage{

        private String data;

        private String desc;

        private String ext;

        private String sound;

        public ImCustomMessage(){
            this.setMsgType("TIMCustomElem");
        }

        @Override
        protected Map msgContent(){
            Map content = new HashMap();
            content.put("Desc", this.desc);
            content.put("Data", this.data);
            content.put("Ext", this.ext);
            content.put("Sound", sound);
            return content;
        }
    }

    public static class ImComplexMessage extends ImMessage{
        private List<ImMessage> messages = new ArrayList<>();

        public void add(ImMessage message){
            this.messages.add(message);
        }

        @Override
        protected List msgBody(){
            List<Map> bodys = new ArrayList<>();
            for(ImMessage message : this.messages){
                Map body = new HashMap();
                body.put("MsgType", message.getMsgType());
                body.put("MsgContent", message.msgContent());
                bodys.add(body);
            }

            return bodys;
        }
    }
}

